package com.zang.blogz.strategy.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zang.blogz.constant.MdConstant;
import com.zang.blogz.enmus.ArticleStatusEnum;
import com.zang.blogz.entity.Article;
import com.zang.blogz.model.output.ArticleSearchVO;
import com.zang.blogz.strategy.SearchStrategy;
import io.github.vampireachao.stream.core.optional.Opp;
import io.github.vampireachao.stream.core.stream.Steam;
import io.github.vampireachao.stream.plugin.mybatisplus.Database;
import io.github.vampireachao.stream.plugin.mybatisplus.Many;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.Objects;


/**
 * @author ZVerify
 * @since 2022/10/11 13:56
 **/
@Service("mySqlSearchStrategyImpl")
public class MySqlSearchStrategyImpl implements SearchStrategy {


    @Override
    public List<ArticleSearchVO> searchArticle(String keywords) {

        // 判空
        if (Opp.ofStr(keywords).isEmpty()){

            return Collections.emptyList();
        }

        // 搜索文章

        List<Article> articles = Many.of(Article::getStatus).eq(ArticleStatusEnum.PUBLIC.getStatus())
                .condition(w -> w.like(Article::getArticleTitle, keywords).or()
                        .like(Article::getArticleContent, keywords)).query();

        // 文章高亮处理

        return Steam.of(articles).map(item -> {

            // 文章内容高亮
            boolean isLowerCase = true;
            String articleContent;
            int contentIndex = item.getArticleContent().indexOf(keywords.toLowerCase());
            if (contentIndex == -1) {
                contentIndex = item.getArticleContent().indexOf(keywords.toUpperCase());
                if (contentIndex != -1) {
                    isLowerCase = false;
                }
            }

            if (contentIndex != -1) {
                // 获取关键词前面的文字
                int preIndex = contentIndex > 15 ? contentIndex - 15 : 0;
                String preText = item.getArticleContent().substring(preIndex, contentIndex);
                // 获取关键词到后面的文字
                int last = contentIndex + keywords.length();
                int postLength = item.getArticleContent().length() - last;
                int postIndex = postLength > 35 ? last + 35 : last + postLength;
                String postText = item.getArticleContent().substring(contentIndex, postIndex);
                // 文章内容高亮
                if (isLowerCase) {
                    articleContent = (preText + postText).replaceAll(keywords.toLowerCase(), MdConstant.PRE_TAG + keywords.toLowerCase() + MdConstant.POST_TAG);
                } else {
                    articleContent = (preText + postText).replaceAll(keywords.toUpperCase(), MdConstant.PRE_TAG + keywords.toUpperCase() + MdConstant.POST_TAG);
                }
            } else {
                return null;
            }

            isLowerCase = true;
            int titleIndex = item.getArticleTitle().indexOf(keywords.toLowerCase());
            if (titleIndex == -1) {
                titleIndex = item.getArticleTitle().indexOf(keywords.toUpperCase());
                if (titleIndex != -1) {
                    isLowerCase = false;
                }
            }
            // 文章标题高亮
            String articleTitle;
            if (isLowerCase) {
                articleTitle = item.getArticleTitle().replaceAll(keywords.toLowerCase(), MdConstant.PRE_TAG + keywords.toLowerCase() + MdConstant.POST_TAG);
            } else {
                articleTitle = item.getArticleTitle().replaceAll(keywords.toUpperCase(), MdConstant.PRE_TAG + keywords.toUpperCase() + MdConstant.POST_TAG);
            }
            return ArticleSearchVO.builder()
                    .id(item.getId())
                    .articleTitle(articleTitle)
                    .articleContent(articleContent)
                    .build();
        }).filter(Objects::nonNull).toList();
    }
}
